//
//  LGBaseViewController.m
//  UniverseProject
//
//  Created by ETOP on 2018/2/25.
//  Copyright © 2018年 Cloud2209. All rights reserved.
//

#import "LGBaseViewController.h"

@interface LGBaseViewController ()<UINavigationControllerDelegate>

@property (nonatomic ,weak) UIImageView *navBarHairlineImageView;

@end

@implementation LGBaseViewController


- (void)setLoadLGNavgationBar:(BOOL)loadLGNavgationBar{
    _loadLGNavgationBar = loadLGNavgationBar;
    
    if (_loadLGNavgationBar) {
        LGNavgationBar *bar = [[LGNavgationBar alloc] initWithFrame:CGRectMake(0, 0, K_SCREEN_WIDTH, k_SafeAreaTopHeight)];
        [self.view addSubview:bar];
        self.lg_navigationBar = bar;
    }
}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    if (_hideNavLine) {
        self.navBarHairlineImageView.hidden = YES;
    }
    
    if (_hideNavBar) {
        // 设置导航控制器的代理为self
        self.navigationController.delegate = self;
    }
    
    if (_NavBarTransprancy) {
        //设置导航栏背景图片为一个空的image，这样就透明了
        [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
        
        //去掉透明后导航栏下边的黑边
        [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
        
    }
    
    if (_gesture_handel) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    }
    
}


- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    if (_hideNavLine) {
        self.navBarHairlineImageView.hidden = NO;
    }
    
    if (_NavBarTransprancy) {
        //    如果不想让其他页面的导航栏变为透明 需要重置
        [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
        [self.navigationController.navigationBar setShadowImage:nil];
    }
}



- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    if (_gesture_handel) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}




- (void)viewDidLoad {
    [super viewDidLoad];
    
//    self.navigationController.navigationBar.translucent = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    
}




#pragma mark - 导航栏按钮设置

- (void)setUpNavRightItem:(NSString *)right{
    
    [self setUpNavItem:right Director:1];
    
}

- (void)setUpNavLeftItem:(NSString *)left{
    [self setUpNavItem:left Director:0];
}


- (void)setUpNavItem:(NSString *)itemTitle Director:(NSInteger)director{
    UIButton *item = [UIButton buttonWithType:UIButtonTypeCustom];
    item.tag = director;
    [item addTarget:self action:@selector(navigationBaritemClick:) forControlEvents:UIControlEventTouchUpInside];
    if ([itemTitle hasSuffix:@".png"] || [itemTitle hasSuffix:@".jpg"]) {
        [item setImage:[self imageWithOriginalRenderingMode:itemTitle] forState:UIControlStateNormal];
        [item setContentHorizontalAlignment:(director + 1)];
        item.frame = CGRectMake(0, 0, 44, 44);
    }else {
        [item setTitle:itemTitle forState:UIControlStateNormal];
        [item setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        item.titleLabel.font = LGFONT(14);
        CGFloat width = [itemTitle sizeWithFont:LGFONT(14)].width;
        item.frame = CGRectMake(0, 0, width, 44);
    }
    
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc]initWithCustomView:item];
    if (director == 0) {//左边
        self.navigationItem.leftBarButtonItem = barButtonItem;
    }else{
        self.navigationItem.rightBarButtonItem = barButtonItem;
    }
}

- (UIImage *)imageWithOriginalRenderingMode:(NSString *)imageName
{
    UIImage *image = [UIImage imageNamed:imageName];
    return [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}


/**
 导航栏点击事件
 */
- (void)navigationBaritemClick:(UIButton *)btn{
    if (btn.tag == 0) { //左边
        [self navigationLeftItemClick];
    }else{
        [self navigationRightItemClick];
    }
}

/**
 导航栏左边点击事件
 */
- (void)navigationLeftItemClick{
    
}

/**
 导航栏右边点击事件
 */
- (void)navigationRightItemClick{
    
}



#pragma mark - 导航栏的线

- (UIImageView *)navBarHairlineImageView
{
    if (!_navBarHairlineImageView) {
        _navBarHairlineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
    }
    return _navBarHairlineImageView;
}

- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}


#pragma mark - 隐藏导航栏
#pragma mark - UINavigationControllerDelegate
// 将要显示控制器
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 判断要显示的控制器是否是自己
    BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
    [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
    NSLog(@"isShowHomePage 导航栏控制 %d",isShowHomePage);
}

@end
